package com.ys.homework;

/**
 * @author CodeYang
 * @Description 5.二维数组遍历求和操作：用二重循环求出二维数组b所有元素的和：
 * int[][] b={{11},{21,22},{31,32,33}}
 * @Date 2020/12/11 20:40
 */
public class 二维数组遍历求和 {
    public static void main(String[] args) {
        int[][] b = {{11}, {21, 22}, {31, 32, 33}};
        System.out.println("求和一:"+sumArr01(b));
        System.out.println("求和二:"+sumArr02(b));
    }

    /**
     * 二维数组遍历求和
     * @param arr
     * @return
     */
    static int sumArr01(int[][] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                sum += arr[i][j];
            }
        }
        return sum;
    }

    /**
     * 遍历求和
     * @param arr
     * @return
     */
    static int sumArr02(int [][] arr){
        int sum=0;
        for (int[] arri : arr) {
            for (int arrj : arri) {
                sum+=arrj;
            }
        }
        return sum;
    }
}
